
<template>
  <div class="container">
    <van-loading
      size="24px"
      vertical
    >等待网页授权结果...</van-loading>
  </div>
</template>
<script>
import Security from '@/common/security'

import { getFingerprintID } from '@/common/systeminfo'
import { Loading } from 'vant'
export default {
  components: {
    [Loading.name]: Loading
  },
  data () {
    return {
      install: 0,
      token: null
    }
  },
  created () {
    this.onAuthent()
  },
  watch: {
    token: {
      handler (v, o) {
        if (v) {
          this.$storage.setAccessToken(v)
          // this.goBack()
        }
      },
      deep: true
    }
  },
  methods: {
    async onAuthent () {
      let systemInfo = this.$storage.get('systemInfo')
      if (!systemInfo) {
        systemInfo = await getFingerprintID()
        this.$storage.set('systemInfo', systemInfo)
      }
      let params = Security.generateSignature({
        sn: systemInfo.sn,
        os: systemInfo.os.name,
        install: this.install,
        model: systemInfo.device ? systemInfo.device.model : ''
      })
      // appid : '2018071144'
      // build : 1
      // install : 0
      // nonce : 'vSvEEcTYY7bLOTDTvMqNRsmesnf6x7ZG'
      // os : 'Windows'
      // sign : '982067fabe17369bb5b02dcb0b27f452eaf89825b6eda6f9cdfc9630da9bdc43'
      // sn : 'ad60f7cd6e2b187f56f23a543ce36ec0'
      // timestamp : '1577188469'
      // this.$http.base.authorize(params).then(res => {
      //   this.token = res.data
      // })
      this.$store
        .dispatch('authorize/system', params)
        .then((res) => {
          console.log(res)
          this.goBack()
          // this.token = res
        })
        .catch((e) => {
          console.log(e)
        })
    },
    goBack () {
      let redirect = this.$route.query.redirect || '/'
      this.$router.replace(redirect)
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="less" scoped>
.van-loading {
  height: 50vh;
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>
